//
//  Team9PhotoViewController.m
//  Team9Project
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9PhotoViewController.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import "Team9PhotoInspirationTableViewCell.h"
#import "Team9CommentInspirationTableViewCell.h"
#import "JQAFNetworkTool.h"
#import "Team9CommentModel.h"
#import "Team9ListModel.h"
#import "Team9ListInspirationTableViewCell.h"
#import "Team9ExpertSectionHeaderView.h"
#import "Team9DesignVC.h"

#import "Team9ExpertCommentModel.h"
#import "Team9ExpertFootCommentView.h"

#define WID 375.0

@interface Team9PhotoViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *photoTableView;
@property (nonatomic, strong) NSMutableArray *photoArr;
@property(nonatomic, strong)NSArray<Team9ExpertCommentModel *> *comments;
@property (nonatomic, strong) NSMutableArray *listArr;

@end

@implementation Team9PhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    
    self.photoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEGHT - 64 - 49) style:UITableViewStylePlain];
    self.photoTableView.delegate = self;
    self.photoTableView.dataSource = self;
    [self.view addSubview:self.photoTableView];
    self.photoTableView.showsVerticalScrollIndicator = NO;
    
    Team9InspirationModel *model = self.photoModel;
    
    if (model.col_remark != nil) {
         UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(10 / WID * WIDTH, 10 / WID * WIDTH, 355 / WID * WIDTH, model.pic_height * WIDTH / model.pic_width + 60 / WID * WIDTH)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 / WID * WIDTH, 10 / WID * WIDTH, 355 / WID * WIDTH, model.pic_height * WIDTH / model.pic_width)];
        self.photoTableView.tableHeaderView = backView;
        [backView addSubview:imageView];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:nil];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10 / WID * WIDTH, model.pic_height * WIDTH / model.pic_width + 10 / WID * WIDTH, 355 / WID * WIDTH, 60 / WID * WIDTH)];
        [backView addSubview:label];
        label.numberOfLines = 0;
        label.font = [UIFont systemFontOfSize:14 / WID * WIDTH];
        label.text = model.col_remark;
    }else if (model.col_remark == nil){
    
        UIImageView *backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 / WID * WIDTH, 10 / WID * WIDTH, 355 / WID * WIDTH, model.pic_height * WIDTH / model.pic_width - 30 / WID * WIDTH)];
        self.photoTableView.tableHeaderView = backImageView;
        [backImageView sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:nil];

    }
    
    
    [self createData];
    
}

- (void)createData{
    NSString *str = [NSString stringWithFormat:@"http://api.meilijia.com/app/photo_info_more?photo_id=%@", self.photoModel.photo_id];
    [JQAFNetworkTool getUrl:str body:nil result:JQJSON headerFile:nil success:^(id result) {
        
        NSMutableDictionary *dic = result[@"data"][@"photo_comments"];
        self.comments = [Team9ExpertCommentModel baseModelByArr:dic[@"comments"]];
        NSMutableDictionary *dic1 = result[@"data"][@"photo_col_history"];
        self.listArr = [NSMutableArray array];
        self.listArr = [Team9ListModel baseModelByArr:dic1[@"list"]].mutableCopy;

        
        [self.photoTableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

#pragma mark tableView分区头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 1) {
         return @"评论";
    }else if (section == 2){
        return @"收入画册";
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return self.comments.count;
    }else if (section == 2){
        return self.listArr.count;
    }
    return 0;
}

#pragma mark tableView自适应高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        Team9InspirationModel *model = self.photoModel;
        return [Team9Tool heightByFloatSize:14 / WID * WIDTH width:355 / WID * WIDTH With:model.col_name] + 165 / WID * WIDTH;
    }else if (indexPath.section == 1) {
        CGFloat width = WIDTH - 100 * WIDTH / 375 - 20;
        Team9ExpertCommentModel *model = self.comments[indexPath.row];
        if (model.comment_parent_id.boolValue) {
            width -= 40 * WIDTH/ 375 + 20;
        }
        CGFloat height = [Team9Tool heightByFloatSize:14 * WIDTH / 375 width:width With:model.content] + 25 * WIDTH / 375 + 20;
        CGFloat lessHeight = 40 * WIDTH / 375 + 20;
        return height > lessHeight ? height : lessHeight;
    }else if (indexPath.section == 2){
        Team9ListModel *model = self.listArr[indexPath.row];
        return [Team9Tool heightByFloatSize:14 / WID * WIDTH width:205 / WID * WIDTH With:model.col_name] + 65 / WID * WIDTH;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *reuse = @"reuse";
        Team9PhotoInspirationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[Team9PhotoInspirationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        Team9InspirationModel *model = self.photoModel;
        [cell.author_faceImageView sd_setImageWithURL:[NSURL URLWithString:model.author_face] placeholderImage:[UIImage imageNamed:@"333"]];
        NSString *str = [NSString stringWithFormat:@"来自%@的晒家画册", model.author_nick];
        
        NSMutableAttributedString *AttributeStr = [[NSMutableAttributedString alloc] initWithString:str];
        NSRange range = [str rangeOfString:model.author_nick];
        [AttributeStr setAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor], NSFontAttributeName:[UIFont systemFontOfSize:0.045 * WIDTH]} range:range];
        cell.author_nickLabel.attributedText = AttributeStr;
        
        cell.col_nameLabel.text = model.col_name;
        [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:model.col_photos[0][@"pic"]] placeholderImage:[UIImage imageNamed:@"111"]];
        [cell.imageView2 sd_setImageWithURL:[NSURL URLWithString:model.col_photos[1][@"pic"]] placeholderImage:[UIImage imageNamed:@"111"]];
        [cell.imageView3 sd_setImageWithURL:[NSURL URLWithString:model.col_photos[2][@"pic"]] placeholderImage:[UIImage imageNamed:@"111"]];
        return cell;
    }else if (indexPath.section == 1){
        Team9ExpertFootCommentView *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseCell"];
        if (!cell) {
            cell = [[Team9ExpertFootCommentView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseCell"];
        }
        cell.vc = self;
        cell.model = self.comments[indexPath.row];
        return cell;
    }else if (indexPath.section == 2){
        static NSString *listReuse = @"listReuse";
        Team9ListInspirationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:listReuse];
        if (!cell) {
            cell = [[Team9ListInspirationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:listReuse];
        }
        Team9ListModel *model = self.listArr[indexPath.row];
        [cell.author_faceImageView sd_setImageWithURL:[NSURL URLWithString:model.author_face] placeholderImage:nil];
        cell.author_nickLabel.text = model.author_nick;
        cell.col_nameLabel.text = model.col_name;
        [cell.col_coverImageView sd_setImageWithURL:[NSURL URLWithString:model.col_cover] placeholderImage:[UIImage imageNamed:@"111"]];
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        Team9DesignVC *designVC = [Team9DesignVC new];
        designVC.url = [NSString stringWithFormat:@"http://api.meilijia.com/app/collection_page2?col_id=%@&tab=photo",_photoModel.col_id];
        
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:designVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }else if (indexPath.section == 2) {
        Team9ListModel *model = self.listArr[indexPath.row];
        Team9DesignVC *designVC = [Team9DesignVC new];
        designVC.url = [NSString stringWithFormat:@"http://api.meilijia.com/app/collection_page2?col_id=%ld&tab=photo", model.col_id];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:designVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;

    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
